package org.qpcr.community.project.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.Resource;
import org.hswebframework.web.authorization.annotation.SaveAction;
import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
import org.qpcr.community.auth.service.UserDetailService;
import org.qpcr.community.project.model.entity.DataBackUpEntity;
import org.qpcr.community.project.model.entity.ExperimentalRecordsInstanceEntity;
import org.qpcr.community.project.service.DataBackUpService;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;


@RestController
@RequestMapping( {"/DataBackUp"})
@Authorize
@Resource(id = "Data-Back", name = "出厂设置备份管理")
@Slf4j
@Tag(name = "出厂设置备份管理")
@RequiredArgsConstructor
public class DataBackUpController implements ReactiveServiceCrudController<DataBackUpEntity, String> {
    @Getter
    private final DataBackUpService service;
    private final UserDetailService userDetailService;

    @PostMapping("/add")
    @SaveAction
    @Operation(
        summary = "新增单个数据,并返回新增后的数据."
    )
    @Authorize(ignore = true)
    public Flux<SaveResult> addOverride(@RequestBody Flux<DataBackUpEntity> payload) {
        return payload.flatMap(entity -> userDetailService.getUserDetailById(entity.getCreatorId()).flatMap(userDetail -> {
            entity.setCreatorUserName(userDetail.getUsername());
            entity.setCreatorName(userDetail.getName());
            return this.save(Flux.from(Mono.just(entity)));
        }));

    }


}
